//
//  ServiceViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class ServiceViewController: KMViewController {

    fileprivate var urlString = ""
    fileprivate let cellId = "serviceCell"
    fileprivate var dataArray = [[String:String]]()
    fileprivate var collectionView : UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        createCollectionView()
        getData()
    }
    
    init(urlString:String,title:String) {
        super.init(nibName: nil, bundle: nil)
        
        self.title = title
        self.urlString = kmHttps + urlString
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 创建collectionView
    fileprivate func createCollectionView() {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = RATIO_H(maxNum: 10)
        layout.minimumInteritemSpacing = RATIO(maxNum: 10)
        layout.itemSize = CGSize.init(width: (ScreenW - RATIO(maxNum: 50))/4, height: RATIO(maxNum: 85))
        layout.sectionInset = UIEdgeInsets.init(top: RATIO_H(maxNum: 10), left: RATIO(maxNum: 10), bottom: RATIO_H(maxNum: 10), right: RATIO(maxNum: 10))
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.showsVerticalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(BankServiceCell.self, forCellWithReuseIdentifier: cellId)
        self.view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    //MARK: 加载数据
    fileprivate func getData() {
        
        KMTool.requestGetData(URLString: self.urlString, parameters: nil) { (data) in
            
            var elements:[String]!
            var attributes:[String]!
            var keys:[String]!
            
            switch self.title {
                case progressInquiry://进度查询
                    elements = ["//div[@class='schedule']/a","//div[@class='schedule']/a/img","//div[@class='schedule']/a/span"]
                    attributes = ["href","src","text"]
                    keys = ["bank_href","img_src","bank_text"]
                case creditCardActivation://信用卡激活
                    elements = ["//div[@class='activation schedule']/a","//div[@class='activation schedule']/a/img","//div[@class='activation schedule']/a/span"]
                    attributes = ["href","src","text"]
                    keys = ["bank_href","img_src","bank_text"]
                default:
                break
            }
            
            self.dataArray = KMTool.htmlToModel(data: data, elements: elements, attributes: attributes, keys: keys)
            self.collectionView.reloadData()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

        
    }

}


extension ServiceViewController : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? BankServiceCell
        cell?.contentView.layer.borderColor = UIColor.black.cgColor
        cell?.contentView.layer.borderWidth = 1
        cell?.titleLabel.text = dataArray[indexPath.row]["bank_text"]
        cell?.headImageView.setUrlImage(with: kmHttps + dataArray[indexPath.row]["img_src"]!, placeholder: UIImage.imageWithName("loading"))
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        let webVC = WebDetailsViewController.init(urlString: dataArray[indexPath.row]["bank_href"], title: nil)
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
}
